﻿using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class FormBglzry : Form
    {
        public FormBglzry()
        {
            InitializeComponent();
        }

        private void button刷新_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("007004001"))
            {
                List工序();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
            
        }

        private void List工序()
        {
            string sql;
            ///最大顺序号的报工单，未入库的
            sql = @"  select '' 行号,z_baogong.生产订单号 生产订单号,z_baogong.加工顺序,z_baogong.工序名称 工序名称,z_baogong.物料编号 物料编号,
                Inventory.cinvname 物料名称,Inventory.cinvstd 物料规格,
                Z_UA_User.cuser_id 报工人员工号,Z_UA_User.cuser_name 报工人员,
                isnull(z_baogong.报工数量,0) 报工数量,z_baogong.报工时间 报工时间   from z_baogong 
              
            join Inventory on Inventory.cinvcode=z_baogong.物料编号
            join Z_UA_User  on Z_UA_User.cUSER_Id=z_baogong.报工人员     where 1=1 ";

            if (this.textBox1.Text != "")
            {
                sql += " and ( z_baogong.物料编号 like '%" + this.textBox1.Text + "%' or Inventory.cinvname like '%" + this.textBox1.Text + "%' )";
            }
            if (this.comboBox工序.Text != "请选择")
            {
                sql += " and z_baogong.工序名称 = '" + this.comboBox工序.Text + "'";
            }
            if (this.textBox3.Text != "")
            {
                sql += " and z_baogong.生产订单号 like '%" + this.textBox3.Text + "%'";
            }

            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and z_baogong.报工时间>='" + this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and z_baogong.报工时间<'" + this.dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }

            sql += "  order by Z_UA_User.cUSER_Id  ";
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["行号"] = (i + 1).ToString();
                }

                //tb的订单合计
                textBox订单数.Text = tb.Select().GroupBy(x => x["生产订单号"]).Count().ToString();
                //tb的合计报工数
                textBox合计报工数.Text = tb.Compute("sum(报工数量)", "").ToString();
            }

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            saveFileDialog.FileName = $"人员报工记录{DateTime.Now.ToString("yyMMdd")}";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                MiniExcel.SaveAs(saveFileDialog.FileName, dataGridView1.DataSource, overwriteFile: true);
                MessageBox.Show("导出成功");
            }

            //if (pub.是否有权限("007004002"))
            //{
            //    string fileName = "";
            //    string saveFileName = "";
            //    SaveFileDialog saveDialog = new SaveFileDialog();
            //    saveDialog.DefaultExt = "xls";
            //    saveDialog.Filter = "Excel文件|*.xls";
            //    saveDialog.FileName = fileName;
            //    saveDialog.ShowDialog();
            //    saveFileName = saveDialog.FileName;
            //    if (saveFileName.IndexOf(":") < 0) return; //被点了取消
            //    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            //    if (xlApp == null)
            //    {
            //        MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");
            //        return;
            //    }
            //    Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            //    Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            //    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            //    //写入标题             
            //    for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //    { worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
            //    //写入数值
            //    for (int r = 0; r < dataGridView1.Rows.Count; r++)
            //    {
            //        for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //        {
            //            worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
            //        }
            //        System.Windows.Forms.Application.DoEvents();
            //    }
            //    worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            //    MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);
            //    if (saveFileName != "")
            //    {
            //        try
            //        {
            //            workbook.Saved = true;
            //            workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
            //        }
            //        catch (Exception ex)
            //        {//fileSaved = false;                      
            //            MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);
            //        }
            //    }
            //    xlApp.Quit();
            //    GC.Collect();//强行销毁           }
            //}
            //else
            //{
            //    MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            //}
        }

        private void FormBglzry_Load(object sender, EventArgs e)
        {
            string sql = $@"select '请选择'  Description
        union all
        select Description from sfc_operation ";

            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
            comboBox工序.DataSource = dt;
            comboBox工序.DisplayMember = "Description";
            comboBox工序.ValueMember = "Description";
            comboBox工序.SelectedIndex = 0;

            //当前电脑的像素
            int width = Screen.PrimaryScreen.Bounds.Width;
            int height = Screen.PrimaryScreen.Bounds.Height;
            if (width < 1080)
            {
                //将表格字体调小
                this.dataGridView1.DefaultCellStyle.Font = new Font("宋体", 9);
            }
        }
    }
}
